package com.logistics.delivery.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.delivery.entity.domain.DeliveryContainer;
import com.logistics.delivery.entity.model.DeliveryContainerRow;
import com.logistics.delivery.entity.model.DeliveryContainerSave;
import com.logistics.delivery.mapper.DeliveryContainerMapper;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.tool.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 配送托盘 服务实现类
 * </p>
 *
 * @author sun.yang
 * @since 2024-06-27
 */
@Service
@Mapper
public class DeliveryContainerService extends ServiceImpl<DeliveryContainerMapper, DeliveryContainer>{

    @Autowired
    private DeliveryContainerMapper deliveryContainerMapper;

    /**
     * 保存配送托盘
     * @param save
     * @throws MessageException
     */
    @Insert
    public void save(DeliveryContainerSave save) throws MessageException {
        if (StringUtils.isNull(save.getId())) {
            this.save(DeliveryContainer.build(save));
        } else {
            this.update(DeliveryContainer.build(save), new LambdaQueryWrapper<DeliveryContainer>()
                    .eq(DeliveryContainer::getId, save.getId()));
        }
    }

    /**
     * 保存多条
     * @param save
     */
    public void save(List<DeliveryContainerSave> save){
        this.saveBatch(save.stream().map(DeliveryContainer::build).collect(Collectors.toList()));
    }


    /**
     * 根据配送单ID查找没配送的托盘
     * @param deliveryId
     * @return
     */
    public List<DeliveryContainerRow> listByDeliveryId(Integer deliveryId){
        return deliveryContainerMapper.findListByDeliveryId(deliveryId).stream().
                map(DeliveryContainerRow::build).collect(Collectors.toList());
    }
}
